package com.aso114.project.mvp.activity.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.adapter.FragmentAdapter;
import com.aso114.project.base.BaseFragment;
import com.aso114.project.commonview.NoSlideViewPager;
import com.aso114.project.mvp.LoginSP;
import com.aso114.project.mvp.activity.ChoiceExamActivity;
import com.aso114.project.mvp.activity.ExamResultsActivity;
import com.aso114.project.mvp.activity.ReadWapActivity;
import com.aso114.project.mvp.activity.SelectedTopicPayActivity;
import com.aso114.project.util.Constant;
import com.aso114.project.util.DensityUtil;
import com.aso114.project.util.Helper;
import com.aso114.project.util.StatusBarUtil;
import com.aso114.project.util.SystemBarHelper;

import java.lang.reflect.Field;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * A simple {@link Fragment} subclass.
 * 题库
 */
public class MainIndexFragment extends BaseFragment {

    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.vp_pager)
    NoSlideViewPager vpPager;
    @BindView(R.id.title_left_tv)
    TextView titleLeftTv;
    @BindView(R.id.layout_title_bar_title_tv)
    TextView layoutTitleBarTitleTv;
    Unbinder unbinder;
    Intent intent;

    ArrayList<String> titleDatas = new ArrayList<>();
    ArrayList<Fragment> fragmentList = new ArrayList<Fragment>();
    @BindView(R.id.layout_title_bar_right_iv)
    ImageView layoutTitleBarRightIv;
    @BindView(R.id.title_right_tv)
    TextView titleRightTv;
    @BindView(R.id.title_top_ly)
    RelativeLayout titleTopLy;
    Dialog dialog;
    Animation animation;
    @BindView(R.id.yuedu)
    ImageView yuedu;

    @Override
    protected int getLayoutResourceId() {

        //实现状态栏图标和文字颜色为暗色    xml文件的根布局中去添加android:fitsSystemWindows="true"
        getActivity().getWindow().getDecorView().setSystemUiVisibility(View
                .SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        StatusBarUtil.setOPPOStatusTextColor(true, getActivity());
        return R.layout.fragment_driving_test;
    }

    @Override
    protected void initView() {
        SystemBarHelper.immersiveStatusBar(getActivity());
        SystemBarHelper.setHeightAndPadding(getActivity(), titleTopLy);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(getActivity(), 0);
            SystemBarHelper.setStatusBarDarkMode(getActivity());
        }
        if (LoginSP.getInstance().isBanner()) {
            Helper.gotoLogin();
            startActivity(new Intent(getContext(), SelectedTopicPayActivity.class));
        }
        if (LoginSP.getInstance().getCityname().equals("")) {
            titleLeftTv.setText("厦门");
        } else {
            String city_name = LoginSP.getInstance().getCityname().replace("考", "");
            titleLeftTv.setText(city_name);
        }

        layoutTitleBarTitleTv.setText("题  库");

        Drawable drawable = getActivity().getResources().getDrawable(R.mipmap.home_xia_ic);
        // 这一步必须要做,否则不会显示.
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        titleLeftTv.setCompoundDrawables(null, null, drawable, null);

        layoutTitleBarRightIv.setImageResource(R.mipmap.home_cj_ic);
        animation = AnimationUtils.loadAnimation(getActivity(), R.anim.img_animation);
        LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
        animation.setInterpolator(lin);
        layoutTitleBarRightIv.startAnimation(animation);
        titleRightTv.setText("成绩");
        titleRightTv.setVisibility(View.VISIBLE);
        layoutTitleBarRightIv.setVisibility(View.VISIBLE);

        titleDatas.add("公务员行测");
        titleDatas.add("公务员申论");
        fragmentList.add(new AdministrarAptitudeTestFragment());
        fragmentList.add(new ExpoundingFragment());
        FragmentAdapter adapter = new FragmentAdapter(getChildFragmentManager(), titleDatas,
                fragmentList);
        vpPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(vpPager);
        tabLayout.setTabsFromPagerAdapter(adapter);
        vpPager.setPagingEnabled(true);//是否可左右滑动
        reflex(tabLayout);

        if (LoginSP.getInstance().getFirstTost().equals("")) {
//            Tost();
        }


        yuedu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ReadWapActivity.startReadWapActivity(getActivity(), true, Constant.YD_URL);
            }
        });


    }


    public void Tost() {

        final View view = getActivity().getLayoutInflater().inflate(
                R.layout.explaintost, null);
        dialog = new Dialog(getActivity(), R.style.dialog);
        dialog.setContentView(view, new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        dialog.getWindow().setGravity(Gravity.CENTER);
        // 设置点击外围解散
        dialog.setCanceledOnTouchOutside(true);//setoncanclelisten
        dialog.show();
        dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialogInterface) {
                LoginSP.getInstance().setFirstTost("1");
            }
        });
        view.findViewById(R.id.submit).setOnClickListener(
                new View.OnClickListener() {
                    public void onClick(View v) {

                        dialog.dismiss();
                        LoginSP.getInstance().setFirstTost("1");

                    }
                });


    }

    @Override
    protected void initData() {
    }

    @Override
    protected void addOnClick() {

    }

    public MainIndexFragment() {
    }


    public void reflex(final TabLayout tabLayout) {
        //了解源码得知 线的宽度是根据 tabView的宽度来设置的
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    int dp10 = DensityUtil.dp2px(tabLayout.getContext(), 30);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距为10dp  注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView
                                .getLayoutParams();
                        params.width = width;
                        params.leftMargin = dp10;
                        params.rightMargin = dp10;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.title_left_tv, R.id.layout_title_bar_title_tv, R.id.layout_title_bar_right_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_left_tv:
                if (Helper.isLogin()) {
                    return;
                }
                intent = new Intent(getContext(), ChoiceExamActivity.class);
                startActivity(intent);
                break;
            case R.id.layout_title_bar_title_tv:
                break;
            case R.id.layout_title_bar_right_iv:
                if (Helper.isLogin()) {
                    return;
                }
                //考试成绩
                intent = new Intent(getContext(), ExamResultsActivity.class);
                intent.putExtra("type", "30");
                startActivity(intent);
                break;


        }
    }


    @Override
    public void onResume() {
        super.onResume();
        SystemBarHelper.immersiveStatusBar(getActivity());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            SystemBarHelper.immersiveStatusBar(getActivity(), 0);
            SystemBarHelper.setStatusBarDarkMode(getActivity());
        }
        layoutTitleBarRightIv.startAnimation(animation);
        if (LoginSP.getInstance().getCityname().equals("")) {
            titleLeftTv.setText("厦门");
        } else {

            if (LoginSP.getInstance().getCityname().equals("国考")) {
                titleLeftTv.setText("国考");
            } else {
                String city_name = LoginSP.getInstance().getCityname().replace("考", "");
                titleLeftTv.setText(city_name);
            }

        }
    }
}

